var userData = {
    tid: undefined, //场馆类型ID
    sid: undefined, //场馆ID
    date: undefined, //预约日期
    endDate: undefined, //预约截至日期
    timeSpans: undefined, //预约时间段
    isRandom: false                             //   ???
}


var userDisplayData = {
    type: undefined, //场馆类型ID
    name: undefined, //场馆ID
    date: undefined, //预约日期
    endDate: undefined, //预约截至日期
    timeSpans: undefined //预约时间段
}


var url = {                          // 定义url类型
    detailedUrl: undefined,
    getStadiumByTypeUrl: undefined,
    getTimeSpansByDateUrl: undefined,
    getSubscribeIndex: undefined,
    subscribe: undefined
}

//用来同步ajax
var getStadiumByType_rs = {
    status: 1
}
var getTimeSpansByDate_rs = {
    status: 1
}
var confirmSubscribe_rs = {
    status: 1
}

var callback = {
    getStadiumByTypeSuccessCallback: "subscribe.getStadiumByType.success",
    getStadiumByTypeCompleteCallback: "subscribe.getStadiumByType.complete",
    getStadiumByTypeBeforeSendCallback: "subscribe.getStadiumByType.beforeSend",

    getTimeSpansByDateSuccessCallback: "subscribe.getTimeSpansByDateSuccessCallback.success",
    getTimeSpansByDateCompleteCallback: "subscribe.getTimeSpansByDateCompleteCallback.complete",
    getTimeSpansByDateBeforeCallback: "subscribe.getTimeSpansByDateBeforeCallback.beforeSend",
    getTimeSpanByDataErrorCallback: "subscribe.getTimeSpansByDateBeforeCallback.error",

    confirmSubscribeSuccessCallback: "subscribe.confirmSubscribeSuccessCallback.success",
    confirmSubscribeBeforeCallback: "subscribe.confirmSubscribeBeforeCallback.beforeSend",
    confirmSubscribeErrorCallback: "subscribe.confirmSubscribeErrorCallback.beforeSend"
}

var subscribe = {
    init: function (options) {
        url.getStadiumByTypeUrl = options.getStadiumByTypeUrl
        url.getTimeSpansByDateUrl = options.getTimeSpansByDateUrl
        url.getSubscribeIndex = options.getSubscribeIndex
        url.subscribe = options.subscribe
        url.detailedUrl = options.detailedUrl
    },
    getStadiumByType: function () {
        iAjax({
            url: url.getStadiumByTypeUrl,
            data: "tid=" + userData.tid,
            rs: getStadiumByType_rs,
            success: function (json) {
                var e = $.Event(callback.getStadiumByTypeSuccessCallback, this)
                e.json = json
                e.tid = userData.tid
                $(document).trigger(e)
            },
            complete: function () {
                var e = $.Event(callback.getStadiumByTypeCompleteCallback, this)
                $(document).trigger(e)
            },
            beforeSend: function () {
                var e = $.Event(callback.getStadiumByTypeBeforeSendCallback, this)
                $(document).trigger(e)
            }
        })
    },
    getTimeSpansByDate: function () {
        iAjax({
            url: url.getTimeSpansByDateUrl,
            data: "date=" + userData.date + "&sid=" + userData.sid,
            rs: getTimeSpansByDate_rs,
            success: function (json) {
                var e = $.Event(callback.getTimeSpansByDateSuccessCallback)
                e.json = json
                e.date = userData.date
                $(document).trigger(e)
            },
            error: function (xhr, data, ex) {

            },
            complete: function () {
                var e = $.Event(callback.getTimeSpansByDateCompleteCallback)
                $(document).trigger(e)
            },
            beforeSend: function () {
                var e = $.Event(callback.getTimeSpansByDateBeforeCallback)
                $(document).trigger(e)
            }
        })
    },
    confirmSubscribe: function (sType) {
        var dataFormat = "startDate={0}&endDate={1}&times={2}&sid={3}&sType={4}&isRandom={5}";
        var data = String.format(dataFormat, userData.date, userData.endDate, userData.timeSpans, userData.sid, sType, userData.isRandom)
        iAjax({
            url: url.subscribe,
            data: data,
            type: 'POST',
            rs: confirmSubscribe_rs,
            success: function (json) {
                var e = $.Event(callback.confirmSubscribeSuccessCallback)
                e.json = json
                e.date = userData.date
                $(document).trigger(e)
            },
            error: function (xhr, error, ex) {
                var e = $.Event(callback.confirmSubscribeErrorCallback)
                e.error = error
                e.ex = ex
                e.xhr = xhr
                $(document).trigger(e)
            },
            beforeSend: function () {
                var e = $.Event(callback.confirmSubscribeBeforeCallback)
                $(document).trigger(e)
            }
        })
    }
}